/**
 * @sw-package after-sales
 */
import { mount } from '@vue/test-utils';
import EntityCollection from 'src/core/data/entity-collection.data';
import FileValidationService from 'src/app/service/file-validation.service';

const orderFixture = {
    id: '1234',
    documents: [],
    taxStatus: 'gross',
    orderNumber: '10000',
    amountNet: 80,
    amountGross: 100,
    lineItems: [],
};

async function createWrapper() {
    return mount(
        await wrapTestComponent('sw-order-document-settings-modal', {
            sync: true,
        }),
        {
            global: {
                stubs: {
                    'sw-modal': {
                        template: '<div class="sw-modal"><slot></slot><slot name="modal-footer"></slot></div>',
                    },
                    'sw-container': {
                        template: '<div class="sw-container"><slot></slot></div>',
                    },
                    'sw-text-field': true,
                    'sw-datepicker': true,
                    'sw-checkbox-field': true,

                    'sw-base-field': await wrapTestComponent('sw-base-field', {
                        sync: true,
                    }),
                    'sw-file-input': await wrapTestComponent('sw-file-input', {
                        sync: true,
                    }),
                    'sw-media-upload-v2': await wrapTestComponent('sw-media-upload-v2', { sync: true }),
                    'sw-context-button': {
                        template: '<div class="sw-context-button"><slot></slot></div>',
                    },
                    'sw-button-group': await wrapTestComponent('sw-button-group', { sync: true }),
                    'sw-context-menu-item': {
                        template: `
                        <div class="sw-context-menu-item" @click="$emit('click', $event.target.value)">
                            <slot></slot>
                        </div>`,
                    },
                    'sw-upload-listener': true,
                    'sw-textarea-field': true,
                    'sw-field-error': true,
                    'sw-media-modal-v2': true,
                    'sw-inheritance-switch': true,
                    'sw-ai-copilot-badge': true,
                    'sw-help-text': true,
                    'router-link': true,
                    'sw-loader': true,
                    'sw-media-url-form': true,
                    'sw-media-preview-v2': true,
                },
                provide: {
                    fileValidationService: new FileValidationService(),
                    numberRangeService: {
                        reserve: () => Promise.resolve({ number: 1000 }),
                    },
                    mediaService: {
                        addListener: () => {},
                        removeByTag: () => {},
                        removeListener: () => {},
                        getDefaultFolderId: () => {},
                    },
                    repositoryFactory: {
                        create: () => ({
                            get: (id) => {
                                return Promise.resolve({
                                    id,
                                    fileSize: 10000,
                                    type: 'application/pdf',
                                });
                            },
                            search: () => {
                                return Promise.resolve(new EntityCollection('', '', Shopware.Context.api, null, [{}], 1));
                            },
                        }),
                    },
                    configService: {
                        getConfig: () =>
                            Promise.resolve({
                                settings: {
                                    enableUrlFeature: false,
                                },
                            }),
                    },
                },
            },
            props: {
                order: orderFixture,
                isLoading: false,
                currentDocumentType: {},
                isLoadingDocument: false,
                isLoadingPreview: false,
            },
        },
    );
}

describe('src/module/sw-order/component/sw-order-document-settings-modal', () => {
    let wrapper;

    beforeEach(async () => {
        wrapper = await createWrapper();
        await flushPromises();
    });

    it('should emit `preview-show` event when click on Preview button', async () => {
        const previewButton = wrapper.find('.sw-order-document-settings-modal__preview-button');

        await previewButton.trigger('click');

        expect(wrapper.emitted()['preview-show']).toBeTruthy();
        expect(wrapper.emitted()['preview-show'][0][1]).toBe('pdf');
    });

    it('should show file or hide custom document file when toggling Upload custom document', async () => {
        const inputUploadCustomDoc = wrapper.find('input[name="sw-field--uploadDocument"]');
        await inputUploadCustomDoc.setChecked(true);

        expect(wrapper.find('sw-upload-listener-stub').exists()).toBeTruthy();
        expect(wrapper.find('.sw-media-upload-v2').exists()).toBeTruthy();
    });

    it('should emit `create` event when click on Create button', async () => {
        const createButton = wrapper.find('.sw-order-document-settings-modal__create');
        await createButton.trigger('click');

        expect(wrapper.emitted()['document-create']).toBeTruthy();
    });

    it('should emit `document-create` event when click on Create and send button', async () => {
        const createAndSendButton = wrapper.find('.sw-order-document-settings-modal__send-button');
        await createAndSendButton.trigger('click');

        expect(wrapper.emitted()['document-create']).toBeTruthy();
        expect(wrapper.emitted()['document-create'][0][1]).toBe('send');
    });

    it('should emit `document-create` event when click on Create and download button', async () => {
        const createAndSendButton = wrapper.find('.sw-order-document-settings-modal__download-button');
        await createAndSendButton.trigger('click');

        expect(wrapper.emitted()['document-create']).toBeTruthy();
        expect(wrapper.emitted()['document-create'][0][1]).toBe('download');
    });

    it('should able to add file from media modal if media is suitable', async () => {
        const customDocumentToggle = wrapper.find('input[name="sw-field--uploadDocument"]');
        await customDocumentToggle.setChecked(true);

        wrapper.vm.onAddMediaFromLibrary([
            {
                id: 'media1',
                fileSize: 10000,
                name: 'test.pdf',
                type: 'application/pdf',
            },
        ]);

        expect(wrapper.vm.documentConfig.documentMediaFileId).toBe('media1');
    });

    it('should able to add file uploaded from url if media is suitable', async () => {
        const customDocumentToggle = wrapper.find('input[name="sw-field--uploadDocument"]');
        await customDocumentToggle.setChecked(true);

        await wrapper.vm.successfulUploadFromUrl({
            targetId: 'media1',
        });

        expect(wrapper.vm.documentConfig.documentMediaFileId).toBe('media1');
    });

    it('should able to show modal title responding to document type', async () => {
        await wrapper.setProps({
            currentDocumentType: {
                id: '1',
                name: 'Invoice',
                technicalName: 'invoice',
            },
        });

        const modal = wrapper.find('.sw-modal');
        expect(modal.attributes().title).toBe('sw-order.documentModal.modalTitle - Invoice');
    });

    it('should emit `preview-show` event when click on Preview of the HTML button', async () => {
        const previewButton = wrapper.findAll('.sw-button-group').at(0);
        await previewButton.find('.sw-order-document-settings-modal__preview-button-html').trigger('click');

        expect(wrapper.emitted()['preview-show']).toBeTruthy();
        expect(wrapper.emitted()['preview-show'][0][1]).toBe('html');
        expect(wrapper.emitted()['preview-show'][0][0].fileTypes).toEqual(['html']);
    });

    it('should allow any text input in the document number field', async () => {
        const documentNumberFieldInput = wrapper.findByLabel('sw-order.documentModal.labelDocumentNumber');
        expect(documentNumberFieldInput.exists()).toBeTruthy();

        await documentNumberFieldInput.setValue('Prefix-1000-Suffix');
        expect(documentNumberFieldInput.element.value).toBe('Prefix-1000-Suffix');
    });

    it('should enable/disable create & preview buttons by documentNumber value', async () => {
        const documentConfig = {
            documentNumber: '',
            documentDate: '2024/03/01',
        };

        await wrapper.setData({
            documentConfig,
        });
        await flushPromises();

        expect(wrapper.find('.sw-order-document-settings-modal__document-number input').element.value).toBe(
            documentConfig.documentNumber,
        );
        expect(wrapper.find('.sw-order-document-settings-modal__document-date input').element.value).toBe(
            documentConfig.documentDate,
        );

        expect(wrapper.find('.sw-order-document-settings-modal__preview-button').attributes()).toHaveProperty('disabled');
        expect(wrapper.find('.sw-order-document-settings-modal__preview-button-arrow').attributes('disabled')).toBe('true');
        expect(wrapper.find('.sw-order-document-settings-modal__create').attributes()).toHaveProperty('disabled');
        expect(wrapper.find('.sw-order-document-settings-modal__create-arrow').attributes('disabled')).toBe('true');

        await wrapper.find('.sw-order-document-settings-modal__document-number input').setValue('1000');
        await flushPromises();

        expect(wrapper.find('.sw-order-document-settings-modal__document-number input').element.value).toBe('1000');

        expect(wrapper.find('.sw-order-document-settings-modal__preview-button').attributes()).not.toHaveProperty(
            'disabled',
        );
        expect(wrapper.find('.sw-order-document-settings-modal__preview-button-arrow').attributes('disabled')).toBe('false');
        expect(wrapper.find('.sw-order-document-settings-modal__create').attributes()).not.toHaveProperty('disabled');
        expect(wrapper.find('.sw-order-document-settings-modal__create-arrow').attributes('disabled')).toBe('false');
    });

    it('should enable/disable create & preview buttons by documentDate value', async () => {
        const documentConfig = {
            documentNumber: '1000',
            documentDate: '',
        };

        await wrapper.setData({
            documentConfig,
        });
        await flushPromises();

        expect(wrapper.find('.sw-order-document-settings-modal__document-number input').element.value).toBe(
            documentConfig.documentNumber,
        );
        expect(wrapper.find('.sw-order-document-settings-modal__document-date input').element.value).toBe(
            documentConfig.documentDate,
        );

        expect(wrapper.find('.sw-order-document-settings-modal__preview-button').attributes()).toHaveProperty('disabled');
        expect(wrapper.find('.sw-order-document-settings-modal__preview-button-arrow').attributes('disabled')).toBe('true');
        expect(wrapper.find('.sw-order-document-settings-modal__create').attributes()).toHaveProperty('disabled');
        expect(wrapper.find('.sw-order-document-settings-modal__create-arrow').attributes('disabled')).toBe('true');

        await wrapper.setData({
            documentConfig: {
                ...documentConfig,
                documentDate: '2024/03/01',
            },
        });
        await flushPromises();

        expect(wrapper.find('.sw-order-document-settings-modal__document-date input').element.value).toBe('2024/03/01');

        expect(wrapper.find('.sw-order-document-settings-modal__preview-button').attributes()).not.toHaveProperty(
            'disabled',
        );
        expect(wrapper.find('.sw-order-document-settings-modal__preview-button-arrow').attributes('disabled')).toBe('false');
        expect(wrapper.find('.sw-order-document-settings-modal__create').attributes()).not.toHaveProperty('disabled');
        expect(wrapper.find('.sw-order-document-settings-modal__create-arrow').attributes('disabled')).toBe('false');
    });
});
